/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.common.output.writer;

import java.io.IOException;
import java.io.Writer;

import org.telosys.common.TelosysRuntimeException;
import org.telosys.common.dataset.DataRow;
import org.telosys.common.dataset.DataSet;

/**
 * Utility class providing methods to write a Dataset in TEXT format <br>
 * <br>
 * To use an OutputStream : Writer writer = new OutputStreamWriter(outputStream);
 * 
 * @author Laurent GUERIN
 *  
 */
public class DataSetTextWriter //extends TelosysObject
{
    private final static String END_OF_LINE = "\n" ;
    
    //-----------------------------------------------------------------------------------
    /**
     * Writes the DataSet as 0..N rows of text 
     * @param writer
     * @param ds
     */
    public static void toText(Writer writer, DataSet ds )
    {
        toText(writer, ds, null, null, null);
    }
    //-----------------------------------------------------------------------------------
    /**
     * Writes the DataSet as 0..N rows of text 
     * @param writer
     * @param ds
     * @param sSeparator the column separator to use 
     */
    public static void toText(Writer writer, DataSet ds, String sSeparator )
    {
        toText(writer, ds, sSeparator, null, null);
    }
    //-----------------------------------------------------------------------------------
    /**
     * Writes the DataSet as 0..N rows of text 
     * @param writer
     * @param ds
     * @param sSeparator the column separator to use
     * @param sHeader the text header 
     * @param sFooter the text footer 
     */
    public static void toText(Writer writer, DataSet ds, String sSeparator, String sHeader, String sFooter)
    {
        if ( writer == null )
        {
            throw new IllegalArgumentException("Writer is null");
        }
        if (ds == null)
        {
            throw new IllegalArgumentException("DataSet is null");
        }
        if (sSeparator == null)
        {
            sSeparator = "|";
        }
        
        try
        {
            if ( sHeader != null )
            {
                writer.write( sHeader );
                writer.write(END_OF_LINE);
            }
            
            int iRowCount = ds.getRowCount();
            DataRow dr = null;
            int iSize = 0;
            for ( int iRow = 1 ; iRow <= iRowCount ; iRow++ )
            {
                dr = (DataRow) ds.getDataRow(iRow);
                if (dr != null)
                {
                    String s = "" ;
                    iSize = dr.getSize();
                    Object obj = null;
                    //--- For each column in the DataRow
                    for ( int iCol = 1 ; iCol <= iSize ; iCol++ )
                    {
                        if ( iCol > 1)
                        {
                            s = sSeparator ;
                        }
                        obj = dr.getObject(iCol);
                        if (obj != null)
                        {
                            writer.write( s + obj.toString() );
                        }
                    }
                    writer.write(END_OF_LINE);
                }
            }
            
            if ( sFooter != null )
            {
                writer.write( sFooter );
                writer.write(END_OF_LINE);
            }            
            writer.flush();
            
        } catch (IOException ex)
        {
            throw new TelosysRuntimeException("toText : IOException", ex);
        }
    }
    //-----------------------------------------------------------------------------------
}